package com.mes.loco.aps.server.controller.sfc;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.SFCService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.bpm.BPMTaskBase;
import com.iplant.aps.service.po.sfc.SFCScrapApprovalBPM;
import com.iplant.aps.service.po.sfc.SFCStationPerson;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 报废评审(流程引擎版)
 *
 * @author PengYouWang
 * 2022-11-22 11:33:46
 */
@RestController
@RequestMapping("/api/SFCScrapApprovalBPM")
public class SFCScrapApprovalBPMController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SFCScrapApprovalBPMController.class);

    final
    SFCService wSFCService;

    public SFCScrapApprovalBPMController(SFCService wSFCService) {
        this.wSFCService = wSFCService;
    }

    /**
     * 查历史记录
     */
    @GetMapping("/History")
    public Object History(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // ①获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wUpFlowID = StringUtils.parseInt(request.getParameter("UpFlowID"));
            int wFollowerID = StringUtils.parseInt(request.getParameter("FollowerID"));
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            int wBOMID = StringUtils.parseInt(request.getParameter("BOMID"));
            int wLevel = StringUtils.parseInt(request.getParameter("Level"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<SFCScrapApprovalBPM>> wServiceResult =
                    wSFCService.SFC_QueryScrapApprovalBPMHistory(wLoginUser, wID, wCode,
                            wUpFlowID, wFollowerID, wOrderID, wPartID, wPartPointID, wBOMID, wLevel, wStartTime,
                            wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 人员获取任务
     */
    @GetMapping("/EmployeeAll")
    public Object EmployeeAll(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wTagTypes = StringUtils.parseInt(request.getParameter("TagTypes"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<BPMTaskBase>> wServiceResult =
                    wSFCService.SFC_QueryScrapApprovalBPMEmployeeAll(wLoginUser,
                            wTagTypes, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待办、已办、发起 报废评审任务
     */
    @GetMapping("/EmployeeAllNew")
    public Object EmployeeAllNew(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wLevel = StringUtils.parseInt(request.getParameter("Level"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            ServiceResult<List<SFCScrapApprovalBPM>> wServiceResult =
                    wSFCService.SFC_QueryScrapApprovalBPMEmployeeAllNew(wLoginUser,
                            wStartTime, wEndTime, wOrderID, wPartID, wLevel, wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<SFCScrapApprovalBPM> wServiceResult = wSFCService.SFC_GetScrapApprovalBPM(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 工位人员获取(工区主管、工艺师)
     */
    @GetMapping("/StationPerson")
    public Object StationPerson(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wStationID = StringUtils.parseInt(request.getParameter("StationID"));

            ServiceResult<SFCStationPerson> wServiceResult = wSFCService.SFC_QueryStationPerson(wLoginUser, wStationID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取待办、已办、发起 偶换件不合格评审任务
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wLevel = StringUtils.parseInt(request.getParameter("Level"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));

            String wMaterialNo = StringUtils.parseString(request.getParameter("MaterialNo"));
            String wMaterialName = StringUtils.parseString(request.getParameter("MaterialName"));

            int wIsLGL = StringUtils.parseInt(request.getParameter("IsLGL"));
            int wIsQualityLoss = StringUtils.parseInt(request.getParameter("IsQualityLoss"));
            int wCovaType = StringUtils.parseInt(request.getParameter("CovaType"));

            ServiceResult<List<SFCScrapApprovalBPM>> wServiceResult =
                    wSFCService.SFC_QueryScrapApprovalBPMList(wLoginUser, wOrderID,
                            wPartID, wLevel, wStatus, wStartTime, wEndTime, wMaterialNo, wMaterialName, wIsLGL,
                            wIsQualityLoss, wCovaType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 更新单据
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            SFCScrapApprovalBPM wData = CloneTool.Clone(wParam.get("data"), SFCScrapApprovalBPM.class);

            ServiceResult<SFCScrapApprovalBPM> wServiceResult = wSFCService.SFC_UpdateScrapApprovalBPM(wLoginUser,
                    wData);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 重新推送SAP
     */
    @GetMapping("/ReSend")
    public Object ReSend(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wTaskID = StringUtils.parseInt(request.getParameter("TaskID"));

            if (wTaskID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult =
                    wSFCService.SFC_ReSendScrapApprovalBPM(wLoginUser,
                            wTaskID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 触发报废评审流程
     */
    @GetMapping("/TriggerScrapReviewProcess")
    public Object TriggerScrapReviewProcess(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wIPTOutMaterialID = StringUtils.parseInt(request.getParameter("IPTOutMaterialID"));

            if (wIPTOutMaterialID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wSFCService.IPT_TriggerScrapReviewProcess(wLoginUser,
                    wIPTOutMaterialID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
